package com.fs.iot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.common.exception.AppException;
import com.fs.iot.bean.dto.DeviceDTO;
import com.fs.iot.bean.query.DeviceQuery;
import com.fs.iot.service.DeviceEntityService;
import com.fs.iot.service.DeviceService;
import com.fs.common.ErrorCodes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备控制器 
 * @author Coder Generator 2025-03-22 15:29:30 
 **/
@RestController
@RequestMapping("/manager/device")
@Slf4j
@Tag(name = "设备控制器")
public class DeviceController {
    @Autowired
    private DeviceEntityService service;

    @Autowired
    private DeviceService deviceService;

    @PostMapping("add")
    @Operation(summary = "新增")
    public void add(@RequestBody @Validated DeviceDTO req) {
        checkBeforeInsert(req);
        deviceService.insert(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    public void update(@RequestBody @Validated DeviceDTO req) {
        checkBeforeUpdate(req);
        req.setStatus(2);
        deviceService.update(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    public DeviceDTO save(@RequestBody @Validated DeviceDTO req) {
        if (StringUtils.isBlank(req.getId())) {
            checkBeforeInsert(req);
            return deviceService.insert(req);
        }
        req.setStatus(2);
        checkBeforeUpdate(req);
        deviceService.update(req);
        return req;
    }

    private void checkBeforeUpdate(DeviceDTO dtp) {
        String code = dtp.getCode();
        if (StringUtils.isNotBlank(code)) {
            List<DeviceDTO> list = service.queryBuilder().eq("code", code).neq("id", dtp.getId()).query();
            if (!list.isEmpty()) {
                throw AppException.of(ErrorCodes.IOT_DEVICE_CODE_DUPLICATED);
            }
        }
    }

    private void checkBeforeInsert(DeviceDTO dto) {
        String code = dto.getCode();
        List<DeviceDTO> list = service.queryBuilder()
                .eq("code", code)
                .query();
        if (!list.isEmpty()) {
            throw AppException.of(ErrorCodes.IOT_DEVICE_CODE_DUPLICATED);
        }
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    public void delete(@PathVariable("id") String id) {
        deviceService.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public DeviceDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<DeviceDTO> pageQuery(@RequestBody DeviceQuery query) {
        IPage<DeviceDTO> page = service.pageQuery(query);
        service.appendOtherInfo(page.getRecords());
        return page;
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<DeviceDTO> query(@RequestBody DeviceQuery query) {
        List<DeviceDTO> list = service.query(query);
        service.appendOtherInfo(list);
        return list;
    }

    @GetMapping("offline")
    public void offline(String id) {
        deviceService.offline(id);
    }

    @GetMapping("publish")
    public void publish(String id) {
        deviceService.publish(id);
    }
}